package com.thwj.project.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thwj.common.annotation.Log;
import com.thwj.common.core.controller.BaseController;
import com.thwj.common.core.domain.AjaxResult;
import com.thwj.common.core.page.TableDataInfo;
import com.thwj.common.enums.BusinessType;
import com.thwj.common.exception.ServiceException;
import com.thwj.common.utils.poi.ExcelUtil;
import com.thwj.project.biz.controller.vo.SelectDataVo;
import com.thwj.project.biz.domain.ActivityCategory;
import com.thwj.project.biz.service.IActivityCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 活动分类Controller
 *
 * @author generate
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/biz/category")
public class ActivityCategoryController extends BaseController {
    @Autowired
    private IActivityCategoryService activityCategoryService;

/**
 * 查询活动分类列表
 */
@PreAuthorize("@ss.hasPermi('biz:category:list')")
@GetMapping("/list")
    public TableDataInfo list(ActivityCategory activityCategory) {
        startPage();
        List<ActivityCategory> list = activityCategoryService.getBaseMapper().selectList(new QueryWrapper<>(activityCategory).orderByDesc("order_weight"));
        return getDataTable(list);
    }

    /**
     * 查询活动分类列表
     */
    @PreAuthorize("@ss.hasPermi('biz:chatroom:list')")
    @GetMapping("/searchList")
    public AjaxResult searchList(ActivityCategory activityCategory) {
        List<ActivityCategory> list = activityCategoryService.list(new QueryWrapper<>(activityCategory));
        if(CollectionUtils.isEmpty(list)) {
            throw new ServiceException("没有数据");
        }
        List<SelectDataVo> selectDataVoList = new ArrayList<>();
        list.forEach(item->{
            selectDataVoList.add(new SelectDataVo(item.getName(), item.getId()));
        });
        return AjaxResult.success(selectDataVoList);
    }
    /**
     * 导出活动分类列表
     */
    @PreAuthorize("@ss.hasPermi('biz:category:export')")
    @Log(title = "活动分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ActivityCategory activityCategory) {
        List<ActivityCategory> list = activityCategoryService.list(new QueryWrapper<>(activityCategory));
        ExcelUtil<ActivityCategory> util = new ExcelUtil<ActivityCategory>(ActivityCategory. class);
        util.exportExcel(response, list, "活动分类数据");
    }

    /**
     * 获取活动分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(activityCategoryService.getById(id));
    }

    /**
     * 新增活动分类
     */
    @PreAuthorize("@ss.hasPermi('biz:category:add')")
    @Log(title = "活动分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActivityCategory activityCategory) {
        return toAjax(activityCategoryService.save(activityCategory));
    }

    /**
     * 修改活动分类
     */
    @PreAuthorize("@ss.hasPermi('biz:category:edit')")
    @Log(title = "活动分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActivityCategory activityCategory) {
        return toAjax(activityCategoryService.updateById(activityCategory));
    }

    /**
     * 删除活动分类
     */
    @PreAuthorize("@ss.hasPermi('biz:category:remove')")
    @Log(title = "活动分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(activityCategoryService.removeByIds(Arrays.asList(ids)));
    }
}
